//
//  HWInviteFriendsViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/10.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWInviteFriendsViewController.h"
#import "HWYouxiFenxiangGameView.h"
#import "HWInviteTheDetailViewController.h"
#import "HWIntegralMallViewController.h" // 积分商城

@interface HWInviteFriendsViewController ()<HWYouxiFenxiangGameViewDelegate>
@property(strong,nonatomic)UIView *yaoQingView;
@property(strong,nonatomic)UILabel *yaoqingLabel;
@property(strong,nonatomic)UILabel *jingfenEdulabel;
@property(strong,nonatomic)UILabel *wodejifenLabel;
@property(strong,nonatomic)UIView *jiangliguizeView; // 奖励规则Uiview
@property(strong,nonatomic) NSDictionary *dataDic;
@property(strong,nonatomic) HWYouxiFenxiangGameView *fenxiangView; // 分享UIview



@end

@implementation HWInviteFriendsViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addControlsView];
    
    // 发送网络请求
    [self sendNetworkRequest];
    
}
 
// 添加控件
- (void)addControlsView {
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self beijingView];
    
    [self navigaView];
    
    [self addbeijingtupiankongjian];
    
    // 添加规则UIVIew
    [self addguizeView];
    
    
    
    
}

// 发送网络请求
- (void)sendNetworkRequest {
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int  t =  [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    
    
    
    
    
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"app_friend",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"sign": signMD5
                                };
  
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
         
            self.dataDic = response[@"data"];
            
            [self qingqiushezhiyixieshuju];
            
            
            
            
            

        } else {
            
             [MBProgressHUD  showError:response[@"message"]];
            
        }
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [MBProgressHUD  showError:@"加载失败"];
        
    }];
    
    
    
    
    
    
    
    
    
}


- (void)qingqiushezhiyixieshuju {
    
    NSString *rnedhu = [NSString stringWithFormat:@"您已邀请%@人加入我们",self.dataDic[@"num"]];
     self.yaoqingLabel.attributedText =  [WMUtil attributedStringWithText:rnedhu withRangText:[NSString stringWithFormat:@"%@",self.dataDic[@"num"]] withColor:RGB(244, 97, 32) withFont:[UIFont systemFontOfSize:16]];
             
             
     NSString *jifen = [NSString stringWithFormat:@"累计获得%@积分",self.dataDic[@"points"]];
     self.jingfenEdulabel.attributedText =  [WMUtil attributedStringWithText:jifen withRangText:[NSString stringWithFormat:@"%@",self.dataDic[@"points"]] withColor:RGB(244, 97, 32) withFont:[UIFont systemFontOfSize:15]];
    
    
    
    NSString *myjifen = [NSString stringWithFormat:@"我的积分：%@",self.dataDic[@"my_points"]];
    self.wodejifenLabel.attributedText =  [WMUtil attributedStringWithText:myjifen withRangText:[NSString stringWithFormat:@"%@",self.dataDic[@"my_points"]] withColor:RGB(244, 97, 32) withFont:[UIFont systemFontOfSize:12]];
    
    
    NSString *nameStr = [NSString stringWithFormat:@"%@",self.dataDic[@"text"]];
    
    NSArray *arr =[nameStr componentsSeparatedByString:@"/n"];
  
    
    
    NSMutableArray  *arrH =  [[NSMutableArray alloc]init];
        for (int i = 0 ; i < arr.count; i++) {
          CGFloat h =  [self  calculateRowHeight:arr[i] fontSize:12];
          [arrH addObject:[NSString stringWithFormat:@"%f",h]];
            
            
        }

       for (int i = 0 ; i < arr.count; i++) {
           
           UILabel *label = [[UILabel alloc] init];
           label.numberOfLines = 0;
         //  label.hidden = YES;
           
           label.font = [UIFont systemFontOfSize:12];
           label.text = arr[i];
           label.frame = CGRectMake(14, 0, kScreenW-18*2-14*2, 30);
           label.textColor = RGB(51, 51, 51);
           [self.jiangliguizeView addSubview:label];
          
           if (i == 0) {
               CGFloat h0 = [arrH[0] floatValue];
              
                label.frame = CGRectMake(14, 45, kScreenW-18*2-14*2, h0);
            
                NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"已通过我们官方审核" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
       
               
                label.attributedText = atrtabl;
               
               
               
               
           } else if (i == 1) {
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               label.frame = CGRectMake(14,45+h0+i*10, kScreenW-18*2-14*2, h1);
               
                self.jiangliguizeView.frame = CGRectMake(18,  self.yaoQingView.bottom+12, kScreenW-18*2, 45+h0+h1+i*10+60);
               
               
           } else if (i == 2){
               
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               CGFloat h2 = [arrH[2] floatValue];
               label.frame = CGRectMake(14,45+ h0+h1+i*10, kScreenW-18*2-14*2, h2);
               
               
           }  else if (i == 3){
               
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               CGFloat h2 = [arrH[2] floatValue];
               CGFloat h3 = [arrH[3] floatValue];
               label.frame = CGRectMake(17,50+ h0+h1+h2+i*10, kScreenW-17*2, h3);
               
               NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"4.交易完成后，不支持退货；" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
               label.attributedText = atrtabl;
               
           } else {
               
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               CGFloat h2 = [arrH[2] floatValue];
               CGFloat h3 = [arrH[3] floatValue];
               CGFloat h4 = [arrH[4] floatValue];
               label.frame = CGRectMake(17, 45+h0+h1+h2+h3+i*10, kScreenW-17*2, h4);
               
        //       self.majixuzhiview.frame = CGRectMake(0, 90+CF_NavHeight, kScreenW, 50+h0+h1+h2+h3+h4+i*10);
               
               
           }
       //    self.majixuzhiview.backgroundColor = [UIColor whiteColor];
           
          
       
       }
    
    
    
    
    
    
    
    
    
    
}






- (void)beijingView {
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    imageView.image = [UIImage imageNamed:@"welfare_share_bg"];
    [self.view addSubview:imageView];
    
    
    
}




- (void)navigaView  {
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight+13, 40, 17);
   // button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight+13, 70, 17)];
    textlabel.text = @"邀请好友";
    textlabel.textAlignment =  NSTextAlignmentCenter;
    textlabel.textColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:textlabel];
    
    
    
    
    
  //  jiludushao
    
       UIButton *lishijilubutton = [UIButton buttonWithType:UIButtonTypeCustom];
       lishijilubutton.frame = CGRectMake(kScreenW-35,StatusBarHeight+10, 20, 20);
      // button.backgroundColor = [UIColor redColor];
       [lishijilubutton setImage:[UIImage imageNamed:@"welfare_share_record_"] forState:UIControlStateNormal];
       [lishijilubutton addTarget:self action:@selector(jiludushaoTap) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:lishijilubutton];
    
    
    
}

- (void)buttonTap {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


// 邀请明细
- (void)jiludushaoTap {
    
    HWInviteTheDetailViewController *inviteTheDetail = [[HWInviteTheDetailViewController alloc]init];
    [self.navigationController pushViewController:inviteTheDetail animated:YES];
    
    
    
}





#pragma mark -------  海量背景
- (void)addbeijingtupiankongjian {
    
    
    UIImageView *jifengimage = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-230)/2, StatusBarHeight+44+22, 230, 81)];
    jifengimage.image = [UIImage imageNamed:@"welfare_share_tit"];
    
    [self.view addSubview:jifengimage];
    
    UIImageView *jingqianImageview  = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-183)/2, jifengimage.bottom-30, 183, 151)];
    jingqianImageview.image = [UIImage imageNamed:@"welfare_share_con"];
    
    [self.view addSubview:jingqianImageview];
    
    
    self.yaoQingView = [[UIView alloc]initWithFrame:CGRectMake(18, jingqianImageview.bottom-55, kScreenW-18*2, 120)];
    self.yaoQingView.layer.cornerRadius = 8;
    self.yaoQingView.layer.masksToBounds = YES;
    self.yaoQingView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.yaoQingView];
    
    self.yaoqingLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 17,  self.yaoQingView.width, 16)];
   
  //  self.yaoqingLabel.text = @"您已邀请 1 人加入我们";
    self.yaoqingLabel.font = [UIFont systemFontOfSize:16];
    self.yaoqingLabel.textColor = RGB(51, 51, 51);
    self.yaoqingLabel.textAlignment =  NSTextAlignmentCenter;
    self.yaoqingLabel.attributedText =  [WMUtil attributedStringWithText:@"您已邀请 1 人加入我们" withRangText:@"1" withColor:RGB(244, 97, 32) withFont:[UIFont systemFontOfSize:16]];
    
    [self.yaoQingView  addSubview:self.yaoqingLabel];
    
    

     self.jingfenEdulabel  = [[UILabel alloc]initWithFrame:CGRectMake(0,  self.yaoqingLabel.bottom+12,  self.yaoQingView.width, 15)];
    
    // self.jingfenEdulabel.text = @"您已邀请 1 人加入我们";
     self.jingfenEdulabel.font = [UIFont systemFontOfSize:16];
     self.jingfenEdulabel.textColor =  RGB(51, 51, 51);
     self.jingfenEdulabel.textAlignment =  NSTextAlignmentCenter;
     self.jingfenEdulabel.attributedText =  [WMUtil attributedStringWithText:@"累计获得 100 积分" withRangText:@"100" withColor:RGB(244, 97, 32) withFont:[UIFont systemFontOfSize:15]];
     
     [self.yaoQingView  addSubview:self.jingfenEdulabel];
    
    
    UIView *fengexianview = [[UIView alloc]initWithFrame:CGRectMake(0, 120-45, kScreenW-18*2, 1)];
    fengexianview.backgroundColor = RGB(244, 244,244);
    [self.yaoQingView  addSubview:fengexianview];
    
    
    self.wodejifenLabel  = [[UILabel alloc]initWithFrame:CGRectMake(16, 120-45,  200, 45)];
      
      // self.jingfenEdulabel.text = @"您已邀请 1 人加入我们";
    self.wodejifenLabel.font = [UIFont systemFontOfSize:12];
    self.wodejifenLabel.textColor = RGB(51, 51, 51);
      // self.wodejifenLabel.textAlignment =  NSTextAlignmentCenter;
    self.wodejifenLabel.attributedText =  [WMUtil attributedStringWithText:@"我的积分：100" withRangText:@"100" withColor:RGB(244, 97, 32) withFont:[UIFont systemFontOfSize:12]];
       
    [self.yaoQingView  addSubview:self.wodejifenLabel];
    
    
    UILabel *jifenduihuanLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.yaoQingView.width-60-10, 120-45,  60, 45)];
    jifenduihuanLabel.textColor = RGB(26, 113, 255);
    jifenduihuanLabel.font = [UIFont systemFontOfSize:13];
    jifenduihuanLabel.text = @"积分兑换";
    jifenduihuanLabel.userInteractionEnabled = YES;
    [self.yaoQingView  addSubview:jifenduihuanLabel];
    
    UITapGestureRecognizer *jifenduihuanges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jifenduihuanTap)];
    [jifenduihuanLabel addGestureRecognizer:jifenduihuanges];
    
    
    
    
}

// 点击积分兑换
- (void)jifenduihuanTap {
    
    
    HWIntegralMallViewController *integra = [[HWIntegralMallViewController alloc]init];
    [self.navigationController pushViewController:integra animated:YES];
    
    
    
    
}



// 添加规则Uiview
-(void)addguizeView  {
    
    
    
    self.jiangliguizeView  = [[UIView alloc]initWithFrame:CGRectMake(18,  self.yaoQingView.bottom+12, kScreenW-18*2, 180)];
    self.jiangliguizeView.layer.cornerRadius = 8;
    self.jiangliguizeView.layer.masksToBounds = YES;
    self.jiangliguizeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.jiangliguizeView];
    
    
    UILabel *jianliguizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW-18*2, 45)];
    jianliguizeLabel.text = @"---奖励规则---";
    jianliguizeLabel.font = [UIFont systemFontOfSize:13];
    jianliguizeLabel.textColor = RGB(244, 97, 32);
    jianliguizeLabel.textAlignment = NSTextAlignmentCenter;
    [self.jiangliguizeView addSubview:jianliguizeLabel];
     
    NSArray *arr = @[@"1.邀请好友注册账号并实名认证后即可获得100积分，上不封顶",@"2.好友在本APP充值平台币，您可以获得充值金额5%的积分奖励。单个好友最多奖励上限500积分，邀请人数不限。"];
       
        NSMutableArray  *arrH =  [[NSMutableArray alloc]init];
        for (int i = 0 ; i < arr.count; i++) {
          CGFloat h =  [self  calculateRowHeight:arr[i] fontSize:12];
          [arrH addObject:[NSString stringWithFormat:@"%f",h]];
            
            
        }

       for (int i = 0 ; i < arr.count; i++) {
           
           UILabel *label = [[UILabel alloc] init];
           label.numberOfLines = 0;
           label.hidden = YES;
           
           label.font = [UIFont systemFontOfSize:12];
           label.text = arr[i];
           label.frame = CGRectMake(14, 0, kScreenW-18*2-14*2, 30);
           label.textColor = RGB(51, 51, 51);
           [self.jiangliguizeView addSubview:label];
          
           if (i == 0) {
               CGFloat h0 = [arrH[0] floatValue];
              
                label.frame = CGRectMake(14, 45, kScreenW-18*2-14*2, h0);
            
                NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"已通过我们官方审核" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
       
               
                label.attributedText = atrtabl;
               
               
               
               
           } else if (i == 1) {
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               label.frame = CGRectMake(14,45+h0+i*10, kScreenW-18*2-14*2, h1);
               
                self.jiangliguizeView.frame = CGRectMake(18,  self.yaoQingView.bottom+12, kScreenW-18*2, 45+h0+h1+i*10+60);
               
               
           } else if (i == 2){
               
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               CGFloat h2 = [arrH[2] floatValue];
               label.frame = CGRectMake(14,45+ h0+h1+i*10, kScreenW-18*2-14*2, h2);
               
               
           }  else if (i == 3){
               
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               CGFloat h2 = [arrH[2] floatValue];
               CGFloat h3 = [arrH[3] floatValue];
               label.frame = CGRectMake(17,50+ h0+h1+h2+i*10, kScreenW-17*2, h3);
               
               NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"4.交易完成后，不支持退货；" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
               label.attributedText = atrtabl;
               
           } else {
               
               CGFloat h0 = [arrH[0] floatValue];
               CGFloat h1 = [arrH[1] floatValue];
               CGFloat h2 = [arrH[2] floatValue];
               CGFloat h3 = [arrH[3] floatValue];
               CGFloat h4 = [arrH[4] floatValue];
               label.frame = CGRectMake(17, 45+h0+h1+h2+h3+i*10, kScreenW-17*2, h4);
               
        //       self.majixuzhiview.frame = CGRectMake(0, 90+CF_NavHeight, kScreenW, 50+h0+h1+h2+h3+h4+i*10);
               
               
           }
       //    self.majixuzhiview.backgroundColor = [UIColor whiteColor];
           
          
       
       }
    
    
    
    
       
        UIButton *lijiyaoqingbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        lijiyaoqingbutton.frame = CGRectMake(65,kScreenH-30-8, kScreenW-65*2, 30);
       // button.backgroundColor = [UIColor redColor];
     //   [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
        lijiyaoqingbutton.backgroundColor =blueBackgroundBT;
        [lijiyaoqingbutton addTarget:self action:@selector(yaoqingTap) forControlEvents:UIControlEventTouchUpInside];
        [lijiyaoqingbutton setTitle:@"立即邀请" forState:UIControlStateNormal];
       lijiyaoqingbutton.layer.cornerRadius = 15;
     lijiyaoqingbutton.layer.masksToBounds = YES;
        [self.view addSubview:lijiyaoqingbutton];
        
   
    
    
    
}


#pragma mark ----------

- (void)yaoqingTap {
    
    
    
    HWYouxiFenxiangGameView *FenXiang = [[HWYouxiFenxiangGameView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    FenXiang.delegate = self;
    self.fenxiangView = FenXiang;
    [kKeyWindow addSubview:FenXiang];
    
    
    NSString *iocSt = [NSString stringWithFormat:@"%@",self.dataDic[@"qrcode"]];
    NSURL *url = [NSURL URLWithString:iocSt];
    [self.fenxiangView.qrcodeImageView sd_setImageWithURL:url completed:nil];
    

    
}


- (void)xuazhongTag:(int )tap {
    
    NSLog(@"%d",tap);
    
    
    
}







- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
 //  NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12]};
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-18*2-14*2, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}


@end
